import {fetch, post} from './index'
import router from '@/router'
import {UserService} from "@/middleware/db/userService";
import {createSocket} from "@/middleware/socket/webSocket";

//  登陆
const httpProtocolUserLogin = '/auth/login';


export async function sendUserLoginHttpRequest(context, requestData) {
    const response = await post(httpProtocolUserLogin, requestData);
    context.commit("onChangeAdminInfo", response.data);
    context.commit("onIsLogin", true);
    context.commit("setUserType", response.data.root);
    UserService.addCurrentUserLogin(response.data);
    requestData.done(response);
    createSocket('ws://192.168.0.43:30099')
}

// 获取用户信息
export function onGetMe(context) {
    const pathname = location.pathname;
    fetch('/admin/me')
        .then(response => {
            context.commit("onIsLogin", true)
            context.commit("onChangeAdminInfo", response.data.data)
            if (location.pathname === '/login' || location.hash.indexOf("#/login") != -1) {
                router.push({path: '/index'})
            }
        })
        .catch(error => {
            console.log(error.response)
            context.commit("onIsLogin", false)
            if (pathname != '/login') {
                router.push({path: '/login'})
            }
        });
}

// 获取配置信息
export function onGetConfigs(context) {
    fetch('/public/configs')
        .then(response => {
            context.commit('onChangeConfigs', response.data.data)
        })
}

// 获取平台配置数据
export function onGetPlatformConfig(context) {
    fetch('/platform/list')
        .then(response => {
            context.commit('onChangePlatformConfig', response.data.data)
        })
}

// 获取systemInfo
export function onGetSystem(context) {
    fetch('/system')
        .then(response => {
            context.commit('onChangeSystemInfo', response.data.data)
            document.title = response.data.data.title
        })
}

// 获取companyInfo
export function onGetCompany(context) {
    fetch('/public/company')
        .then(response => {
            context.commit('onChangeCompanyInfo', response.data.data)
        })
}

// 获取uploads/config
export function onGetUploadsConfig(context) {
    fetch('/uploads/config')
        .then(response => {
            context.commit('onChangeUploadsConfigs', response.data.data)
        })
}

// 获取会话列表
export function onGetContacts(context) {
    fetch('/contact/list')
        .then(response => {
            context.commit('onChangeContacts', response.data.data)
        })
}

// 一分钟上报一次我的活动
export function onRunLastActivity() {
    fetch('/public/activity/')
}

// 获取机器人列表
export function onGetRobots(context) {
    fetch('/robot/list')
        .then(response => {
            context.commit('onChangeRobos', response.data.data)
        })
}

// 获取工单系统counts
export function onGetWorkOrderCounts(context) {
    fetch('/workorder/counts')
        .then(response => {
            context.commit('onChangeWorkOrderCounts', response.data.data)
        })
}







